package com.xuan.job.handler;


import com.github.pagehelper.PageHelper;
import com.xuan.common.core.constants.CacheConstants;
import com.xuan.common.redis.service.RedisService;
import com.xuan.job.mapper.UserSubmitMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
@Slf4j
public class QuestionJobHandler {


    @Resource
    private UserSubmitMapper userSubmitMapper;

    @Resource
    private RedisService redisService;

    @XxlJob(value = "refreshHotQuestionRankHandler")
    public void refreshHotQuestionRankHandler() {
        log.info("*** 更新题目热榜排行开始 ***");
        // 1. 统计用户提交次数前 10 的 questionId (以 tb_user_submit 表为准)
        PageHelper.startPage(1, 10);
        List<Long> questionIdList = userSubmitMapper.listHotQuestionId();
        // 2. redis 中添加数据(q:h:l )
        if (CollectionUtils.isEmpty(questionIdList)) {
            return;
        }
        redisService.deleteObject(CacheConstants.HOT_QUESTION_LIST_KEY);
        redisService.rightPushAll(CacheConstants.HOT_QUESTION_LIST_KEY, questionIdList);
    }

}
